<template>
  <div>
    <div class="container">
      <div class="handle-box">
        <el-button
          type="primary"
          @click="handleExport"
          v-if="slePermissions.includes('sys:logerror:export')"
          >导出</el-button
        >
      </div>
      <el-table
        :data="tableData"
        border
        class="table"
        ref="multipleTable"
        header-cell-class-name="table-header"
        @sort-change="sortChange"
      >
        <el-table-column
          align="center"
          :show-overflow-tooltip="true"
          prop="requestUri"
          label="请求URI"
        ></el-table-column>
        <el-table-column
          align="center"
          :show-overflow-tooltip="true"
          prop="requestMethod"
          label="请求方式"
        ></el-table-column>
        <el-table-column
          align="center"
          :show-overflow-tooltip="true"
          prop="requestParams"
          label="请求参数"
        ></el-table-column>
        <el-table-column
          align="center"
          :show-overflow-tooltip="true"
          prop="ip"
          label="操作IP"
        ></el-table-column>
        <el-table-column
          align="center"
          :show-overflow-tooltip="true"
          prop="userAgent"
          label="用户代理"
        ></el-table-column>

        <el-table-column
          sortable
          align="center"
          :show-overflow-tooltip="true"
          prop="createDate"
          label="创建时间"
        ></el-table-column>
        <el-table-column label="操作" width="180" align="center">
          <template slot-scope="scope">
            <el-button type="text" @click="handleErr(scope.$index, scope.row)"
              >异常信息</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          background
          layout="total,sizes, prev, pager, next, jumper"
          :page-sizes="[5, 10, 20, 30]"
          @size-change="handleSizeChange"
          :current-page="query.pageIndex"
          :page-size="query.pageSize"
          :total="pageTotal"
          @current-change="handlePageChange"
        ></el-pagination>
      </div>
    </div>

    <!-- 异常信息弹出框 -->
    <el-dialog title="异常信息" :visible.sync="errVisible" width="50%">
      <el-form ref="form" :model="form" label-width="auto">
        <p v-text="form.errorInfo"></p>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
// import { sysLogErrorMixin } from "../../common/js/logManage";
// console.log(sysLogErrorMixin);
import { mapGetters } from "vuex";
export default {
  name: "syslogerror",
  // mixins: [sysLogErrorMixin],

  data() {
    return {
      slePermissions: [],
      query: {
        order: "",
        orderField: "",
        pageIndex: 1,
        pageSize: 20
      },
      tableData: [
        {
          id: "",
          requestUri: "",
          requestMethod: "",
          requestParams: null,
          userAgent: "",
          ip: "",
          errorInfo: "",
          createDate: null
        }
      ],
      errVisible: false,
      pageTotal: 0,
      form: {
        errorInfo: "111"
      }
    };
  },
  methods: {
    // 获取数据
    getSLEData({
      order = this.query.order,
      orderField = this.query.orderField,
      page = this.query.pageIndex,
      limit = this.query.pageSize,
      _t = Date.now()
    }) {
      this.$axios
        .get("/sys/log/error/page", {
          params: {
            order,
            orderField,
            page,
            limit,
            _t
          }
        })
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return;
          }
          this.pageTotal = res.data.total || 0;
          this.tableData = res.data.list;
        })
        .catch(() => {});
    },
    // 表头排序
    sortChange({ column, prop, order }) {
      // console.log(column, prop, order);
      prop = prop === "createDate" ? "create_date" : prop;
      this.$set(this.query, "orderField", prop);
      if (order === "ascending") {
        this.$set(this.query, "order", order.substr(0, 3));
        this.getSLEData({});
      } else if (order === "descending") {
        this.$set(this.query, "order", order.substr(0, 4));
        this.getSLEData({});
      }
    },
    getToken(token) {
      var arr = token.split(";");
      for (var i = 0; i < arr.length; i++) {
        if (arr[i].split("=")[0].trim() === "token") {
          return arr[i].split("=")[1];
        }
      }
    },
    // 导出
    handleExport() {
      this.$axios
        .get("/sys/log/error/export", {
          params: {
            token: this.getToken(document.cookie)
          },
          responseType: "blob"
        })
        .then(res => {
          console.log(res);
          // 导出excel表格
          const link = document.createElement("a");
          let blob = new Blob([res.data], { type: "application/vnd.ms-excel" });
          link.style.display = "none";
          link.href = URL.createObjectURL(blob);
          let num = "";
          for (let i = 0; i < 10; i++) {
            num += Math.ceil(Math.random() * 10);
          }
          link.setAttribute("download", "用户_" + num + ".xls");
          document.body.appendChild(link);
          link.click();
          document.body.removeChild(link);
        })
        .catch(error => {
          this.$Notice.error({ title: "错误", desc: "网络连接错误" });
          // console.log(error);
        });
    },
    // 异常信息
    handleErr(index, row) {
      this.errVisible = true;
      // console.log(index, row);
      this.form.errorInfo = row.errorInfo;
    },
    // 分页导航
    handlePageChange(val) {
      this.$set(this.query, "pageIndex", val);
      this.getSLEData({});
    },
    handleSizeChange(val) {
      this.$set(this.query, "pageSize", val);
      this.getSLEData({});
    }
  },
  created() {
    this.getSLEData({});
    this.slePermissions = this.permissions;
  },
  computed: {
    ...mapGetters(["permissions"])
  },
  watch: {
    "$route.path": function(newVal, oldVal) {
      if (newVal == "/sys-log-error") {
        this.slePermissions = this.permissions;
      }
    },
    // 刷新浏览器时触发
    permissions: function(newVal, oldVal) {
      // console.log(newVal, oldVal);
      this.slePermissions = newVal;
    }
  }
};
</script>

<style scoped>
@import url("../../common/css/index.css");
</style>
